package com.zygk.library.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import com.zygk.library.util.ImageManager;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseListAdapter<E> extends BaseAdapter {
    private List<E> mList = new ArrayList<E>();
    protected Context mContext;
    protected Activity mActivity;
    protected LayoutInflater mInflater;
    protected ImageManager mImageManager;

    public BaseListAdapter(Context context) {
        mContext = context;
        mActivity = (Activity) context;
        mInflater = LayoutInflater.from(context);
        mImageManager = new ImageManager(context);
    }

    public BaseListAdapter(Context context, List<E> list) {
        this(context);
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    public void clearAll() {
        mList.clear();
    }

    public List<E> getData() {
        return mList;
    }

    public void addALL(List<E> lists){
        if(lists==null||lists.size()==0){
            return ;
        }
        mList.addAll(lists);
    }
    public void add(E item){
        mList.add(item);
    }
    public void add(E item, int pos){
        mList.add(pos, item);
    }

    @Override
    public E getItem(int position) {
        return (E) mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void remove(E e){
        mList.remove(e);
    }
    public void remove(int pos){
        mList.remove(pos);
    }

    //刷新单条记录
    public void refreshOneRecord(E e, int pos) {
        getData().set(pos, e);
        notifyDataSetChanged();
    }

    //删除单条记录
    public void removeOneRecord(int pos) {
        getData().remove(pos);
        notifyDataSetChanged();
    }

    //添加单条记录
    public void addOneRecord(E e) {
        getData().add(e);
        notifyDataSetChanged();
    }
    //添加单条记录
    public void addOneRecord(E e, int pos) {
        getData().add(pos, e);
        notifyDataSetChanged();
    }

    public void setData(List<E> list) {
        clearAll();
        addALL(list);
        notifyDataSetChanged();
    }

    public void setData(List<E> list, boolean isLoadMore) {
        if (!isLoadMore) {
            clearAll();
        }
        addALL(list);
        notifyDataSetChanged();
    }
}
